#include "NaTools.hpp"
#include <fstream>
using namespace std;

const double day[] = {0.0, 6.0, 10.0, 13.0, 17.0, 20.0, 28.0};
const double sp1[] = {6.67, 17.3, 42.7, 37.3, 30.1, 29.3, 28.7};
const double sp2[] = {6.67, 16.1, 18.9, 15.0, 10.6, 9.44, 8.89};
vector<double> day_dat;
ofstream outfile;

double N = 45000;

const double step = 1e-3;
class SP1:public func{
	double operator()(double x){
		int i=0;
		for(i=0;i<7;i++) if(x==day[i]) break;
		return sp1[i];
	}
};
class SP2:public func{
	double operator()(double x){
		int i=0;
		for(i=0;i<7;i++) if(x==day[i]) break;
		return sp2[i];
	}
};

void init(){
	for(int i=0;i<7;i++) day_dat.push_back(day[i]);
}

void check(polynomial& Q){
	cout << "The value at day[i]:" << endl;
	for(int i=0;i<7;i++) cout << Q(day[i]) << " "; cout << endl;
}

void plot(polynomial& Q){
	outfile << "[" ;
	double tl = 0;
	for(int i=0;i<=N;i++){
		if(i==N) outfile << Q(tl);
		else outfile << Q(tl) << ",";
		tl += step;
	}
	outfile << "]" << endl;
}

int main(){
	
	init();
	SP1 data1; SP2 data2;
	Newton_inter h1(data1,day_dat),h2(data2,day_dat);
	polynomial Q = h1.stdrize(), R = h2.stdrize();
	//check(Q); check(R);
	//h1.latex(); h2.latex();

	N = 30000;
	outfile.open("plot_E_A.dat");
	outfile << N << " " << day[0] << " " << day[0]+N*step << endl;  
	plot(Q); plot(R);
	outfile.close();

	N = 45000;
	outfile.open("plot_E_B.dat");
	outfile << N << " " << day[0] << " " << day[0]+N*step << endl; 
	plot(Q); plot(R);
	outfile.close();

	cout << "The average weight of sample 1 in the 43th days is " << Q(43) << "." <<endl;
	cout << "The average weight of sample 2 in the 43th days is " << R(43) << "." <<endl;
	
}